package edu.nf.llmtest.common.sync.impl;

import edu.nf.llmtest.common.converter.EssayDTOConverter;
import edu.nf.llmtest.common.converter.UserConverter;
import edu.nf.llmtest.common.enumeration.RedisKeyEnum;
import edu.nf.llmtest.common.sync.DataSyncStrategy;
import edu.nf.llmtest.model.document.EssayDoc;
import edu.nf.llmtest.model.dto.EssayDTO;
import edu.nf.llmtest.model.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ Author bin
 * @ Create  2025/11/7 11:37
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class EssaySyncStrategy  implements DataSyncStrategy<EssayDTO> {

    private final RedisTemplate<String, Object> redisTemplate;

    private final EssayDTOConverter essayConverter;

    private final ElasticsearchOperations operations;

    @Override
    public boolean supports(Class<?> clazz) {
        return EssayDTO.class.equals(clazz);
    }

    @Override
    public void syncToRedis(EssayDTO entity) {
        redisTemplate.opsForValue().set(RedisKeyEnum.ESSAY_PERFIX.getValue() + entity.getEId(), entity);
        log.info("同步用户到 Redis: {}", entity.getEId());
    }

    @Override
    public void deleteFromRedis(EssayDTO entity) {
        redisTemplate.delete(RedisKeyEnum.ESSAY_PERFIX.getValue() + entity.getEId());
        log.info("从 Redis 删除用户: {}", entity.getEId());
    }

    @Override
    public void syncToES(EssayDTO entity) {
        operations.save(essayConverter.convertEssayDTODoc(entity));
        log.info("同步用户到 Elasticsearch: {}", entity.getEId());
    }

    @Override
    public void deleteFromES(EssayDTO entity) {
        operations.delete(entity.getEId());
        log.info("从 Elasticsearch 删除用户: {}", entity.getEId());
    }
}
